import {ref} from "vue"
import { ElMessage,ElLoading } from "element-plus";
import type { FormInstance } from "element-plus"
import { useStore } from "@/store/piniaAutoImport";

export const useFunc=()=>{
    let formModel = ref({
        username:"",
        password:"",
        code:"",
        rememberMe:false
    })
    let formRef = ref()
    let code = ref()
    let picCodeRef = ref()
    let globaLoading = ref(false)
    let useUser = useStore("useUser")

    const handleSubmit = ()=>{
        const loading = ElLoading.service({
            lock:true,
            text:'Loading',
            background:'rgba(0,0,0,0.7)',
        })
        let from = {
            username:formModel.value.username,
            password:formModel.value.password,
        }
        useUser.Login(from).then(res=>{
        globaLoading.value = true;
        setTimeout(()=>{
            loading.close()
            ElMessage({
                message:'登录成功',
                type:'success',
                plain:true,
            })
            globaLoading.value = false;
        },2000)
        }).catch(err=>{
            ElMessage({
                message:'登录失败',
                type:'error',
                plain:true,
            })
        })
        
    }

    const handleReset = (formRef:FormInstance | undefined)=>{
        if (!formRef) return
        formRef.resetFields()
    }


    return {formModel,formRef,code,picCodeRef,globaLoading,handleSubmit,handleReset}
}